package linter.linterjava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import linter.linterjava.POJO.DO.Curriculum;
import linter.linterjava.POJO.DO.CurriculumPlan;
import linter.linterjava.POJO.DTO.CurriculumPlanDTO;
import linter.linterjava.POJO.UtilDTO.PageInfoDTO;
import linter.linterjava.mapper.CurriculumMapper;
import linter.linterjava.service.CurriculumPlanService;
import linter.linterjava.mapper.CurriculumPlanMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static linter.linterjava.custom.ConstantPublicWay.getList;
import static linter.linterjava.custom.ConstantPublicWay.getPageInfo;
import static linter.linterjava.custom.CustomResultType.*;

/**
* @author 北煜
* @description 针对表【curriculum_plan(课程进度完成表)】的数据库操作Service实现
* @createDate 2024-07-07 12:15:25
*/
@Service
public class CurriculumPlanServiceImpl extends ServiceImpl<CurriculumPlanMapper, CurriculumPlan>
    implements CurriculumPlanService{

    @Resource
    CurriculumMapper curriculumMapper;
    @Resource
    CurriculumPlanMapper curriculumPlanMapper;

    @Override
    public String curriculumPlan(CurriculumPlanDTO curriculumPlanDTO, Long uuid) {
        Date date = new Date();
        CurriculumPlan curriculumPlan = new CurriculumPlan();
        CurriculumPlan isExist = curriculumPlanMapper.selectOne(new QueryWrapper<CurriculumPlan>()
                .eq("user_id", uuid).eq("curriculum_id", curriculumPlanDTO.getCurriculumId()));
        if (isExist == null){
            curriculumPlan.setCurriculumId(curriculumPlanDTO.getCurriculumId());
            curriculumPlan.setUserId(uuid);
            curriculumPlan.setSchedule(0);
            curriculumPlan.setCreateTime(date);
            curriculumPlan.setUpdataTime(date);
            int flag = curriculumPlanMapper.insert(curriculumPlan);
            return flag > 0 ? ADD_SUCCESS : ADD_ERROR;
        }else {
            curriculumPlan.setSchedule(curriculumPlanDTO.getSchedule());
            curriculumPlan.setUpdataTime(date);
            int flag = curriculumPlanMapper.update(curriculumPlan, new QueryWrapper<CurriculumPlan>()
                    .eq("curriculum_id", curriculumPlanDTO.getCurriculumId()).eq("user_id", uuid));
            return flag > 0 ? UPDATE_SUCCESS : UPDATE_ERROR;
        }
    }

    @Override
    public Integer getCurriculumPlan(CurriculumPlanDTO curriculumPlanDTO, Long uuid) {
        return curriculumPlanMapper.selectOne(new QueryWrapper<CurriculumPlan>()
                .eq("user_id", uuid).eq("curriculum_id", curriculumPlanDTO.getCurriculumId()))
                .getSchedule();
    }

    @Override
    public PageInfo<Curriculum> getCurriculumFinish(CurriculumPlanDTO curriculumPlanDTO, Long uuid) {
        PageInfoDTO pageInfoDTO = new PageInfoDTO();
        pageInfoDTO.setPageNum(curriculumPlanDTO.getPageNum());
        pageInfoDTO.setPageSize(curriculumPlanDTO.getPageSize());
        List<Long> curriculumIds = getList(new QueryWrapper<CurriculumPlan>()
                .eq("user_id", uuid).eq("schedule",1).eq("is_delete",0),
                curriculumPlanMapper)
                .stream().map(CurriculumPlan::getCurriculumId).collect(Collectors.toList());
        return getPageInfo(pageInfoDTO, new QueryWrapper<Curriculum>().in("id",curriculumIds).eq("is_delete",0),
                              curriculumMapper);
    }

    @Override
    public PageInfo<Curriculum> getLearningCurriculum(CurriculumPlanDTO curriculumPlanDTO, Long uuid) {
        List<Long> curriculumIds = getList(new QueryWrapper<CurriculumPlan>()
                .eq("user_id", uuid).eq("is_delete",0).lt("schedule",1),curriculumPlanMapper)
                .stream().map(CurriculumPlan::getCurriculumId).collect(Collectors.toList());
        PageInfoDTO pageInfoDTO = new PageInfoDTO();
        pageInfoDTO.setPageNum(curriculumPlanDTO.getPageNum());
        pageInfoDTO.setPageSize(curriculumPlanDTO.getPageSize());
        return getPageInfo(pageInfoDTO, new QueryWrapper<Curriculum>().in("id",curriculumIds).eq("is_delete",0)
                ,curriculumMapper);
    }
}




